import { proxy } from 'hono/proxy'
import config from '../config'

export class ProxyController {
  constructor(opt) {
    this.conf = opt.conf
  }

  proxy(c){
    return this.request(c.req, c.req.param('path'))
  }

  request(req, path){
    const query = new URLSearchParams(req.query()).toString()
    const url = `${config.proxyUrl}/${path}${query ? '?' + query : ''}`

    return proxy(url, {
      ...req,
      headers: {
        ...req.header(),
        'X-Forwarded-For': '127.0.0.1',
        'X-Forwarded-Host': req.header('host'),
        // Authorization: undefined, // do not propagate request headers contained in req.header('Authorization')
      },
    })
  }
}